package com.ruoyi.inspection.config;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.Method;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.constant.GlobalConstants;
import com.ruoyi.inspection.utils.AesGcm256Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.*;

@Slf4j
@Component
public class GovernTaskConfig {
    /** 内网正式-前置机地址 **/
    public static String ip = "http://172.24.0.49/send";

    /** 外网正式地址 **/
//    public static String ip = "https://ydbg.yjglj.shanghai.gov.cn:8089/api";
    //生产环境（请求157服务器，程序不停接收处理请求，CPU居高不下，网关问题，一次请求被不停发送，该地址作废）
    //public static String ipJT = "http://192.168.93.157:81";
    //联调环境
    //public static String ipJT = "http://172.24.0.49/sendOne";
    //public static String ip = "https://www.baosight-aq.com:8080/api";

    /**
     * 下发专项检查任务
     */
    public static String checkTaskList = "/v1/data/distribute/distributeCheckTaskList";
    /**
     * 下发专项检查任务企业
     */
    public static String distributeCheckCompanyList = "/v1/data/distribute/distributeCheckCompanyList";
    /**
     * 下发专项检查项
     */
    public static String distributeCheckItemList = "/v1/data/distribute/distributeCheckItemList";
    /**
     * 下发专项检查项评分细则
     */
    public static String distributeCheckScoreList = "/v1/data/distribute/distributeCheckScoreList";
    /**
     * 上报专项检查情况记录
     */
    public static String specialInvestigationRecord = "/v1/data/receive/receiveCheckRecord";
    /**
     * 上报专项检查隐患信息数据
     */
    public static String specialInvestigation = "/v1/data/receive/receiveCheckDanger";
    /**
     * 上报隐患信息数据
     */
    public static String investigation = "/v1/data/receive/receiveDangerInvestigationList";

    /**
     * 上报视频智能分析数据
     * @return
     */
    public static String receiveVideoAlarmList = "/v1/data/oilGasStorage/receiveVideoAlarmList";
    /**
     * 上报雷电预警设备基础信息
     * @return
     */
    public static String receiveThunderWarningEquipList = "/v1/data/oilGasStorage/receiveThunderWarningEquipList";
    /**
     * 上报雷电预警数据
     * @return
     */
    public static String receiveThunderWarningList = "/v1/data/oilGasStorage/receiveThunderWarningList";
    /**
     * 上报环境监测数据
     * @return
     */
    public static String receiveThunderEnvironMonitorList = "/v1/data/oilGasStorage/receiveThunderEnvironMonitorList";


    /**
     * 上报人员基础信息
     * @return
     */
//    public static String receiveEmployeeList = "/v1/data/oilGasStorage/receiveEmployeeList";
    public static String receiveEmployeeList = "/v1/data/person-position/receiveEmployeeList";
    /**
     * 上报电子围栏(区域边界)信息
     * @return
     */
//    public static String receiveAreaList = "/v1/data/personPosition/receiveAreaList";
    public static String receiveAreaList = "/v1/data/person-position/receiveZoneInfoList";
    /**
     * 上报人员实时定位数据
     * @return
     */
//    public static String receiveEmployeePositionList = "/v1/data/oilGasStorage/receiveEmployeePositionList";
    public static String receiveEmployeePositionList = "/v1/data/person-position/receiveEmployeePositionList";
    /**
     * 上报人员聚集数据
     * @return
     */
    public static String receivePersonCrowdList = "/v1/data/person-position/receiveEmployeeGatherInfoList";

    public static String getToken = "/v1/data/auth/credentials/accessToken";
    public static String error = "/v1/data/receive/getRequestLog";
    public static String getInfo = "/v1/data/receive/getLog";

    /**
     * 上报特殊作业票证信息
     * @return
     */
    public static String receiveJobPlanUrl = "/v1/data/special-operation/receiveWorkorderJobList";
    public static String receiveJobTicketUrl = "/v1/data/special-operation/receiveWorkorderList";
    public static String receiveJobVideoUrl = "/v1/data/special-operation/receiveVideoWorkorderList";
    public static String receiveJobAttachmentUrl = "/v1/data/special-operation/receiveWorkorderAttachmentList";

    public static String token() {
        String url = ip + getToken;
        Map<String, String> map = new HashMap<>();
        map.put("app_id", "06e9f82867b44277b296801e160b876e");
        map.put("app_secret", "a53c1b4a05514058b1bc2c4f7808afcf");
        map.put("company_code", GlobalConstants.COMPANY_CODE);
        HttpResponse execute = HttpRequest
                .post(url)
                .charset("UTF-8")
                .body(JSONUtil.toJsonStr(map))
                .timeout(3000)
                .execute();
        int status = execute.getStatus();
        if (status != 200) {
            return null;
        }
        String body = execute.body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        Integer code = jsonObject.getInt("code");
        if (code != 200) {
            return null;
        }
        JSONObject obj = jsonObject.getJSONObject("obj");
        String accessToken = obj.getStr("access_token");
        return accessToken;
    }

    public static String pushData(String data, String url) {
        log.info("推送市局端 url:{} 请求参数:{}", url, data);
        String token = token();
        if (StrUtil.isEmpty(token)) {
            log.error("获取token信息异常");
            return "获取token信息异常";
        }
        Map<String, String> map = new HashMap<>();
        map.put("data", AesGcm256Util.encrypt(data));
        HttpResponse execute = HttpRequest
                .post(ip + url)
                .charset("UTF-8")
                .body(JSONUtil.toJsonStr(map))
                .contentType(" application/json")
                .header(" UniTrust-Token", token)
                .header(" UniTrust-AppId", "06e9f82867b44277b296801e160b876e")
                .timeout(5000)
                .execute();
        System.out.println(execute.getStatus());
        System.out.println(execute.toString());
        int status = execute.getStatus();
        if (status != 200) {
            return "请求失败";
        }
        String body = execute.body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        Integer code = jsonObject.getInt("code");
        if (code != 200) {
            log.error("推送市局端错误 url:{} 返回信息:{}", url, jsonObject.toString());
            return StrUtil.format("推送市局端错误 url:{} 返回信息:{}请求参数:{}", ip + url, jsonObject.toString(), data);
        } else {
            log.info("推送市局端成功 url:{} 返回信息:{}", url, jsonObject.toString());
            return StrUtil.format("推送市局端成功 url:{} 返回信息:{} 请求参数:{}", ip + url, jsonObject.toString(), data);
        }
    }

    public static String getErrorInfos(String... ids) {
        String token = token();
        if (StrUtil.isEmpty(token)) {
            log.error("获取token信息异常");
            return "获取token信息异常";
        }
        if (ids == null || ids.length == 0) {
            log.error("查询推送信息错误 url:{} 参数异常", error);
            return "查询推送信息错误 url:{} 参数异常";
        }

        Map<String, Object> map = new HashMap<>();
        map.put("errorIds", ids);
        HttpResponse execute = HttpRequest
                .post(ip + error)
                .charset("UTF-8")
                .body(JSONUtil.toJsonStr(map))
                .contentType(" application/json")
                .header(" UniTrust-Token", token)
                .header(" UniTrust-AppId", "06e9f82867b44277b296801e160b876e")
                .timeout(3000)
                .execute();
        int status = execute.getStatus();
        if (status != 200) {
            return "请求失败";
        }
        String body = execute.body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        Integer code = jsonObject.getInt("code");
        if (code != 200) {
            log.error("查询推送信息错误 url:{} 返回信息:{}", error, jsonObject.toString());
            return StrUtil.format("推送市局端错误 url:{} 返回信息:{}", error, jsonObject.toString());
        } else {
            log.info("查询推送信息成功 url:{} 返回信息:{}", error, jsonObject.toString());
            return StrUtil.format("推送市局端成功 url:{} 返回信息:{}", error, jsonObject.toString());
        }
    }

    public static String getErrorIds(String batchId) {
        String token = token();
        if (StrUtil.isEmpty(token)) {
            log.error("获取token信息异常");
            return "获取token信息异常";
        }
        List<String> list = new ArrayList<>();
        list.add(batchId);
        Map<String, Object> map = new HashMap<>();
        map.put("batchId", list);
        HttpResponse execute = HttpRequest
                .post(ip + getInfo)
                .charset("UTF-8")
                .body(JSONUtil.toJsonStr(map))
                .contentType(" application/json")
                .header(" UniTrust-Token", token)
                .header(" UniTrust-AppId", "06e9f82867b44277b296801e160b876e")
                .timeout(3000)
                .execute();
        int status = execute.getStatus();
        if (status != 200) {
            return "请求失败";
        }
        String body = execute.body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        Integer code = jsonObject.getInt("code");
        if (code != 200) {
            log.error("查询推送信息错误 url:{} 返回信息:{}", ip + error, jsonObject.toString());
            return StrUtil.format("推送市局端错误 url:{} 返回信息:{}", error, jsonObject.toString());
        } else {
            log.info("查询推送信息成功 url:{} 返回信息:{}", ip + error, jsonObject.toString());
            return StrUtil.format("推送市局端成功 url:{} 返回信息:{}", error, jsonObject.toString());
        }
    }


    public static JSONObject invokePost(String url, Map<String, Object> param) {
        String token = token();
        if (StrUtil.isEmpty(token)) {
            log.error("获取token信息异常");
            return null;
        }

//        log.error("调用市局接口, 请求参数:{}", JSONUtil.toJsonStr(param));
        HttpResponse execute = HttpRequest
                .post(ip + url)
                .charset("UTF-8")
                .body(JSONUtil.toJsonStr(param))
                .contentType(" application/json")
                .header(" UniTrust-Token", token)
                .header(" UniTrust-AppId", "06e9f82867b44277b296801e160b876e")
//                .timeout(3000)
                .execute();
        int status = execute.getStatus();
        if (status != 200) {
            return null;
        }
        String body = execute.body();
        JSONObject jsonObject = JSONUtil.parseObj(body);
        Integer code = jsonObject.getInt("code");
        if (code != 200) {
            log.error("调用市局接口错误 url:{} 返回信息:{}", ip + url, jsonObject.toString());
            return null;
        } else {
            log.info("调用市局接口成功 url:{} 返回信息:{}", ip + url, jsonObject.toString());
            return jsonObject;
        }
    }




//    public static String pushDataJT(String data, String url) {
//        log.info("推送市局端 url:{} 请求参数:{}", url, data);
//        HttpResponse execute = HttpRequest
//                .post(ipJT + url)
//                .charset("UTF-8")
//                .body(data)
//                .contentType(" application/json")
//                .timeout(5000)
//                .execute();
//
//        log.info("推送市局端 url:{} 返回信息:{}", url, execute.body());
//
//        int status = execute.getStatus();
//        if (status != 200) {
//            return "请求失败";
//        }
//        String body = execute.body();
//        JSONObject jsonObject = JSONUtil.parseObj(body);
//        Integer code = jsonObject.getInt("status");
//        if (code != 0) {
//            log.error("推送集团错误 url:{} 返回信息:{}", url, jsonObject.toString());
//            return StrUtil.format("推送集团错误 url:{} 返回信息:{}请求参数:{}", ip + url, jsonObject.toString(), data);
//        } else {
//            log.info("推送集团成功 url:{} 返回信息:{}", url, jsonObject.toString());
//            return StrUtil.format("推送集团成功 url:{} 返回信息:{} 请求参数:{}", ip + url, jsonObject.toString(), data);
//        }
//    }

//    public static JSONObject invokePost2(String url, Map<String, Object> param) {
//        String token = token();
//        if (StrUtil.isEmpty(token)) {
//            log.error("获取token信息异常");
//            return null;
//        }
////         .sendRequestData(task.getUrl(), params.toJSONString(), Method.POST);
//
//
//        HttpResponse execute = HttpRequest
//                .post(ip + url)
//                .charset("UTF-8")
//                .body(JSONUtil.toJsonStr(param))
//                .contentType(" application/json")
//                .header(" UniTrust-Token", token)
//                .header(" UniTrust-AppId", "06e9f82867b44277b296801e160b876e")
//                .timeout(3000)
//                .execute();
//        int status = execute.getStatus();
//        if (status != 200) {
//            return null;
//        }
//        String body = execute.body();
//        JSONObject jsonObject = JSONUtil.parseObj(body);
//        Integer code = jsonObject.getInt("code");
//        if (code != 200) {
//            log.error("调用市局接口错误 url:{} 返回信息:{}", ip + url, jsonObject.toString());
//            return null;
//        } else {
//            log.info("调用市局接口成功 url:{} 返回信息:{}", ip + url, jsonObject.toString());
//            return jsonObject;
//        }
//    }



}
